<?php

class NewsletterController extends BaseClass
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $title;	//Page Title
	public $layout = 'main';
	
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions' => array('index', 'view', 'delete', 'process', 'admin', 'update'),
				'users' => $this->getAdmins(),
			),
			array('deny', // deny other user to perform actions
					'actions' => array('index', 'view', 'delete', 'process', 'admin', 'update'),
					'users' => array('*'),
			),
		);
	}
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}
	public function actionIndex(){
		$this->title = Yii::app()->name." - Newsletter";
		$model=new Newsletters();
		$this->render('index',array(
			'model'=>$model,
		));
	}
	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();
	
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function actionProcess(){
		$model = new Newsletters();
		if($_POST['pageAction'] == 'delete')
		{
			if($model->deleteByPk($_POST['contact'])) {
				Yii::app()->user->setFlash('success','Record (s) Successfully Deleted');
				$this->redirect(Yii::app()->getBaseUrl().'/admin/newsletter');
			}
			else {
				Yii::app()->user->setFlash('error','Record (s) not Deleted');
				$this->redirect(Yii::app()->getBaseUrl().'/admin/newsletter');
			}
		}
	}
	
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Cms the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Newsletters::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}
